
//-----------------------------------------------------
//ItemInsert
//This function takes the selected option from the select 
//box and inserts it into the new select box
//-----------------------------------------------------
function ItemInsert(sElName)
{
 	var fDoFill = true;	
	var nFromLength,nToLength;
	var nOutputOptionPosition;
	var counter;
	var oVoteForm;

	try {
	    oVoteForm = document.forms[0];
	}
	catch (err) {
	    alert(err);
	}

	nOutputOptionPosition = -1;
	nFromLength = oVoteForm.select_from.length;
	nToLength = oVoteForm.select_to.length;
	counter = nToLength + 1;
	
	//iterate options to look for selected
	for (var i = 0; i < nFromLength; i++) {

	    //if selected drop in	
	    if (oVoteForm.select_from.options[i].selected) {
	        //check the list in the new select box if no match then fill.				
	        for (var j = 0; j < nToLength; j++) {
	            if (oVoteForm.select_from.options[i].value == oVoteForm.select_to.options[j].value) {
	                alert(oVoteForm.select_to.options[j].text + " is already No. " + (j + 1) + " on the list.");

	                oVoteForm.select_to.selectedIndex = j;

	                fDoFill = false;
	                nOutputOptionPosition = j;

	            }
	        }
	        if (fDoFill) {
	            var oNewOption = new Option(counter + ". " + oVoteForm.select_from.options[i].text, oVoteForm.select_from.options[i].value);

	            oVoteForm.select_to.options[oVoteForm.select_to.options.length] = oNewOption;
	            nOutputOptionPosition = oVoteForm.select_to.options.length;

	            counter++;
	        }
	    }
	}


	return nOutputOptionPosition;

}






//-----------------------------------------------------
//ItemScrub
//This function deletes the selected item from the select box
//-----------------------------------------------------
function ItemScrub(sElName) {
    var nToLength;
    var sHolder
    var sNumberHolder;
    var sNameHolder;
    var fDirtyList;
    var istart, iend, counter;

    istart = 0;
    fDirtyList = false;
    counter = 1;

    try {
        oVoteForm = document.forms[0];
    }
    catch (err) {
        alert(err);
    }


    nToLength = oVoteForm.select_to.length;

    //iterate options to look for selected
    for (var i = nToLength - 1; i >= 0; i--) {
        //if selected drop in and scrub list
        if (oVoteForm.select_to.options[i].selected) {

            istart = i;
            fDirtyList = true;
            oVoteForm.select_to.options[i] = null;
        }
    }


    //renumber the list
    if (fDirtyList) {
        for (var j = istart; j < nToLength-1; j++) {
            sHolder = oVoteForm.select_to.options[j].text;

            //get name of Item to be renumbered
            sNameHolder = sHolder.substr(sHolder.indexOf(".") + 1);
            //set number from last good location
            sNumberHolder = istart + counter;

            //set option
            oVoteForm.select_to.options[j].text= sNumberHolder+'.'+sNameHolder;
            counter++;
        }
    }

}






//-----------------------------------------------------
//MoveMe
//This Function will move the selected item up or down in the 
//select box
//-----------------------------------------------------
function MoveMe(direction, sElName) {

    var modDir;
    var nToLength;
    var oHolder;
    var iNew;
    var posOld, posNew;
    var sNameNew, sNameOld;
    var oVoteForm

    try {
        oVoteForm = document.forms[0];
    }
    catch (err) {
        alert(err);
    }

    nToLength = oVoteForm.select_to.length;

    switch (direction) {
        case "up":
            modDir = -1;
            break;
        case "down":
            modDir = 1;
            break;
    }

//iterate options to look for selected
    for (var i = 0; i < nToLength; i++) {
        //if selected drop in	
        if (oVoteForm.select_to.options[i].selected) {
            //set new position			
            iNew = i + modDir;

            //check to see if new position is within the box
            if (iNew >= 0 && iNew <= nToLength - 1) {
                //here we move the items
                //hold the item to be displaced  in the temp var oHolder				
                var oHolderNew = new Option(oVoteForm.select_to.options[iNew].text, oVoteForm.select_to.options[iNew].value);
                var oHolderOld = new Option(oVoteForm.select_to.options[i].text, oVoteForm.select_to.options[i].value);

                posNew = oHolderNew.text.substr(0, oHolderNew.text.indexOf("."));
                sNameNew = oHolderOld.text.substr(oHolderOld.text.indexOf(".") + 1);

                posOld = oHolderOld.text.substr(0, oHolderOld.text.indexOf("."));
                sNameOld = oHolderNew.text.substr(oHolderNew.text.indexOf(".") + 1);

                oHolderOld.text = posNew + "." + sNameNew;
                oHolderNew.text = posOld + "." + sNameOld;

                //switch moving item into the new position
                oVoteForm.select_to.options[iNew] = oHolderOld;


                //switch displaced item into the old position
                oVoteForm.select_to.options[i] = oHolderNew;
            } 
        }
    }

    //refocus on the moved item
    oVoteForm.select_to.selectedIndex=iNew;

    return iNew;

}




//------------------------------------------------
//AutoPositionOption
//This function will take a finish position as a parameter 
//and call the MoveMe function until it is at the correct position
//------------------------------------------------
function AutoPositionOption(sElName, newPos, sWorkOnSelectSuffix) {
    //setting newPos to true position in the collection
    newPos = newPos - 1

    var iNewPosition;
    var fStop;
    var nToLength;
    var fValid;
    var nCurOptionPosition = 0;
    var sDirection;
    var oVoteForm;

    try {
        oVoteForm = document.forms[0];
    }
    catch (err) {
        alert(err);
    }

    sDirection = "up";
    fValid = true;

    fStop = false;

    //check for which select box has control
    if (sWorkOnSelectSuffix == "_to") {
        //get selected position
        if (oVoteForm.select_to.selectedIndex > -1) {
            fValid = true;
            nCurOptionPosition = oVoteForm.select_to.selectedIndex;
            //set nToLength
            nToLength = oVoteForm.select_to.length;
            //set direction		
            if (newPos > nCurOptionPosition) sDirection = "down";
        }
        else {
            fValid = false;
        }


    }
    else {

        if (oVoteForm.select_from.selectedIndex > -1) {
            //Try to insert new option - returns index of matching item on the to list
            //of the selected item on the from list
            //or else it returns true if item is able to be inserted
            //if true then we want to set position to end of list
            //otherwise set it to correct option
            fValid = true;
            nCurOptionPosition = ItemInsert(sElName);

            if (nCurOptionPosition >= 0) {
                //get new length
                nToLength = oVoteForm.select_to.length;

                //check return value, true is inserted, otherwise it returns the 
                //current position of the element that exists
                if (fValid) {
                    //select item to last in list
                    oVoteForm.select_to.selectedIndex = nToLength - 1;
                    //now set nCurOptionPosition position
                    nCurOptionPosition = nToLength;
                }
                else {
                    //select correct option
                    oVoteForm.select_to.selectedIndex = nCurOptionPosition;
                    //set direction
                    if (newPos > fValid) sDirection = "down";

                }
            }
        }
        else { fValid = false; }
    } 
    
    
	//find current length	
	if(fValid)
	{
		//check for a valid move, cancel flag if not.
		if(nToLength <= parseInt(newPos)){fStop=true;}
		
		//Move and loop
		while(!fStop)
		{
			iNewPosition = MoveMe(sDirection,sElName);
			
			if(iNewPosition== parseInt(newPos))
			{				
				fStop=true;
			}
			
		}
	}
}



function DoSubmit() {

    var sselect_from;
    var sMsgHeader;
    var sMsgFooter;
    var iloop;
    var oVoteForm;

    try {
        oVoteForm = document.forms[0];
    }
    catch (err) {
        alert(err);
    }
    
    sMsgHeader = "You have selected:\n\n";
    sMsgFooter = "\n\nIs this correct?";
    sselect_from = "";


    for (iloop = 0; iloop < (oVoteForm.select_to.options.length); iloop++) {
        if (iloop > 24) { break; }

        sselect_from = sselect_from  + oVoteForm.select_to.options[iloop].text + "\n";
    }

    if (confirm(sMsgHeader + sselect_from + sMsgFooter)) {

        for (var j = 0; j < oVoteForm.select_to.options.length; j++) {
            oVoteForm.select_to.options[j].selected = true;
        }

        //scrub list
        oVoteForm.select_from.options.length = 0;

        oVoteForm.submit();
    }

    return;
}
